#!/usr/bin/env python
# -*-coding: utf8 -*-
'''
# created by: iori
# created on: Tue 10 Dec 2013 10:00:02 PM CST
'''
import time
import argparse
from utils import voa51_backup
def range_args(low, high):
    class RangeAction(argparse.Action):
        def __call__(self, parser, args, values, option_string=None):
            if len(values)<low or len(values)>high:
                msg = 'argument {arg} needs between {low} and {high} arguments'.format(arg=self.dest, low=low, high=high)
                raise argparse.ArgumentTypeError(msg)
            setattr(args, self.dest, values)
    return RangeAction


def main():
    parser = argparse.ArgumentParser(description="51voa standard english down")
    parser.add_argument('--url',help="assign a url to download")
    parser.add_argument('-d', '--days', default=1, type=int, help='in how many days to download')
    parser.add_argument('-l', '--list', nargs='+', action=range_args(1,3), help="list [start [end]]")
    parser.add_argument('-n', '--net', nargs=1,  choices=['baidu'],  help="assign where to backup")
    parser.add_argument('-c', '--local', default='.',  help='local path')
    args = parser.parse_args()
    print args
    if args.url:
        days = args.days if args.days else 1
        voa51_backup(args.url, days)
    if args.list:
        base = args.list[0]
        base = '{0}@@{1}'.format(base[:base.rfind('_')+1], base[base.rfind('.'):])
        start =int( args.list[1]) if len(args.list)>1 else 1
        end = int(args.list[2]) if len(args.list)>2 else 10
        for i in range(start, end+1):
            url = base.replace('@@', str(i))
            print url
            if args.local:
                voa51_backup(url, days=100, storage='local', path=args.local)
            if args.net:
                vao51_backup(url, day=100, storage=args.net)
            time.sleep(5)

if __name__ == '__main__':
    main()
